/* eslint-disable handle-callback-err */
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import Vuex from 'vuex';
import VueRouter from 'vue-router';
import axios from 'axios';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App';
import Index from './views/index/index';
import Student from './views/student/student';
import Course from './views/course/course';
import ChooseCourse from './views/choosecourse/choosecourse';
import Payment from './views/payment/payment';
import ClassRecord from './views/classrecord/classrecord';
import Material from './views/material/material';
import 'font-awesome/css/font-awesome.min.css';
import Mock from './mock/mock';

import '../static/UE/ueditor.config.js'
import '../static/UE/ueditor.all.min.js'
import '../static/UE/lang/zh-cn/zh-cn.js'
import '../static/UE/ueditor.parse.min.js'
import moment from 'moment'

// 定义一个全局过滤器实现日期格式化
Vue.filter('datefmt', function (input, fmt) {
  // 使用momentjs这个日期格式化类库实现日期的格式化功能
  return moment(input).format(fmt);
});

Mock.mockData();
Vue.use(VueRouter);// 安装路由功能
/* eslint-disable no-new */
Vue.use(VueRouter);
axios.defaults.baseURL = 'http://127.0.0.1:8080';
// axios.defaults.baseURL = 'http://58.87.79.88:8080';
Vue.prototype.$http = axios;
Vue.use(ElementUI);
Vue.use(Vuex);

export default new Vuex.Store({
  modules: {
    Student,
    Course,
  }
});

// const REFRESH_TOKEN_URL = '/token';

// Add a request interceptor
Vue.prototype.$http.interceptors.request.use((config) => {
  // if (config.url.indexOf('/token') === -1) {
  //   let fd = new FormData();
  //   fd.append("scope", "TEACHER");
  //   fd.append("grant_type", "password");
  //   fd.append("username", "admin");
  //   fd.append("password", "123456");
  //   axios({
  //     method: 'POST',
  //     headers: {
  //       'Content-Type': 'application/x-www-form-urlencoded',
  //     },
  //     url: REFRESH_TOKEN_URL,
  //     auth: {
  //       username: 'teacher_web',
  //       password: 'nmzszxsl'
  //     },
  //     data: fd,
  //   }).then((res) => {
  //     config.headers.Authorization = 'Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ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w4yWa7OCnG652g';
  return config;
}, (error) => {
  return Promise.reject(error);
});

// Add a response interceptor
// Vue.prototype.$http.interceptors.response.use((res) => {
//   if (res.data.code === 200) {
//     if (res.data) {
//       return res;
//     }
//   }
//   if (res.data.code === 10031001) {
//     return res;
//   }
//   if (res.data.code === 10011039 || res.data.code === 10011040 || res.data.code === 10011041) {
//     // 验证token失败 || 解析header失败 || 页面已过期,请重新登录
//     // store.dispatch('delete_user_info');
//     // window.location.href = '/';
//     return Promise.reject(res);
//   } else {
//     // alert(res.data.message);
//     return Promise.reject(res);
//   }
// }, (error) => {
//   if (error.response) {
//     // console.error('error: ', error.response);
//     if (error.response.status === 500) {
//       alert(error.response.data.message);
//     } else if (error.response.status === 401) {
//       alert('您无访问权限');
//     } else {
//       // console.log('Error', error.message);
//       alert('接口请求失败或超时！请刷新重试');
//     }
//   } else {
//     alert('接口请求失败或超时！请刷新重试');
//   }
// });

let routes = [
  {
    path: '/',
    component: App,
    children: [
      {path: '/student', component: Student, name: '学生信息', class: 'fa-child'},
      {path: '/course', component: Course, name: '课程信息', class: 'fa-table'},
      {path: '/choose/course', component: ChooseCourse, name: '学生选课', class: 'fa-file'},
      {path: '/payment', component: Payment, name: '学生缴费', class: 'fa-dollar'},
      {path: '/class/record', component: ClassRecord, name: '学生签到', class: 'el-icon-edit'},
      {path: '/material', component: Material, name: '材料信息', class: 'fa-table'},
      {path: '/index', component: Index, name: 'index', class: 'fa-line-chart'},
    ]
  }
];

let router = new VueRouter({
  // 'mode': 'history', 去掉URL的#号，需要配置服务器http://router.vuejs.org/zh-cn/essentials/history-mode.html
  'linkActiveClass': 'active',
  routes
});

new Vue({
  router,
}).$mount('#app');
